package com.demo.woziqi;

import com.demo.woziqi.dao.UserDAO;
import com.demo.woziqi.entity.User;
import com.demo.woziqi.util.GameConstant;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-05-23
 * <p>
 * 用户注册信息录入界面
 */
public class GameRegisterUserStage extends Stage {
    private Button OkBtn;// 确认
    private Button exBtn;// 取消
    private Label username; // 账号
    private Label password; // 密码
    private Label RePassword; // 密码
    private TextField nameField;// 账号
    private TextField pwdField;// 密码
    private TextField RePwdField;// 密码

    private Label userNameLabel; // 姓名提示
    private Label pwdLabel; // 密码提示
    private Label rePwdLabel; // 密码提示

    private Hyperlink link = new Hyperlink("已有账号？返回登录"); // 返回登录超链接

    public static Stage inputStage;

    public GameRegisterUserStage() {
        /*
         * 初始化注册窗体
         * */
        inputStage = this;
        setTitle("用户注册");
        Pane panes = new Pane();
        this.setResizable(false); // 固定大小
        setHeight(375);
        setWidth(500);
        /*
         * 初始化按钮以及超链接等节点
         * */
        this.username = new Label("账   号：");
        this.password = new Label("密   码：");
        this.RePassword = new Label("确认密码：");
        this.nameField = new TextField();
        this.pwdField = new PasswordField();
        this.RePwdField = new PasswordField();
        this.OkBtn = new Button("注    册");
        this.exBtn = new Button("取    消");
        this.userNameLabel = new Label();
        this.pwdLabel = new Label();
        this.rePwdLabel = new Label();
        /*
         * 按钮，超链接，Label定位
         * */
        Font font = Font.font("KaiTi", FontWeight.BOLD, FontPosture.ITALIC, 25);
        username.setLayoutX(50);
        username.setLayoutY(40);
        username.setFont(font);
        username.setTextFill(Color.BLACK);
        nameField.setLayoutX(180);
        nameField.setLayoutY(40);
        nameField.setPromptText("请输入账号");
        Font fonts = Font.font("KaiTi", FontWeight.BOLD, FontPosture.ITALIC, 18);
        link.setLayoutX(300);
        link.setLayoutY(300);
        link.setFont(fonts);

        password.setLayoutX(50);
        password.setLayoutY(110);
        password.setFont(font);
        password.setTextFill(Color.BLACK);
        pwdField.setLayoutX(180);
        pwdField.setLayoutY(110);
        pwdField.setPromptText("请输入密码");

        RePassword.setLayoutX(50);
        RePassword.setLayoutY(180);
        RePassword.setFont(font);
        RePassword.setTextFill(Color.BLACK);
        RePwdField.setLayoutX(180);
        RePwdField.setLayoutY(180);
        RePwdField.setPromptText("确认密码");

        /*
         * userNameLabel 提示Label
         * */
        Font fon = Font.font("KaiTi", FontWeight.BOLD, FontPosture.ITALIC, 18);
        userNameLabel.setLayoutX(180);
        userNameLabel.setLayoutY(80);
        userNameLabel.setTextFill(Color.RED);
        userNameLabel.setFont(fon);

        pwdLabel.setLayoutX(180);
        pwdLabel.setLayoutY(150);
        pwdLabel.setTextFill(Color.RED);
        pwdLabel.setFont(fon);

        rePwdLabel.setLayoutX(180);
        rePwdLabel.setLayoutY(220);
        rePwdLabel.setTextFill(Color.RED);
        rePwdLabel.setFont(fon);

        OkBtn.setLayoutX(110);
        OkBtn.setLayoutY(250);
        OkBtn.setTextFill(Color.BLACK);
        exBtn.setLayoutX(300);
        exBtn.setLayoutY(250);
        exBtn.setTextFill(Color.BLACK);
        /*
         *
         * 将节点添加到面板
         * */
        panes.getChildren().add(username);
        panes.getChildren().add(nameField);
        panes.getChildren().add(password);
        panes.getChildren().add(pwdField);
        panes.getChildren().add(RePassword);
        panes.getChildren().add(RePwdField);
        panes.getChildren().add(exBtn);
        panes.getChildren().add(OkBtn);
        panes.getChildren().add(link);
        panes.getChildren().add(userNameLabel);
        panes.getChildren().add(pwdLabel);
        panes.getChildren().add(rePwdLabel);

        Scene scene = new Scene(panes); // 场景，存放面板
        scene.getStylesheets().add(String.valueOf(getClass().getClassLoader().getResource("login.css"))); //引入css
        this.setScene(scene); // 将场景设置到主舞台

        OkBtn.setOnMouseClicked(e -> { //确定按钮鼠标点击事件
            if (nameField.getText().isEmpty()) {  // 数据有效性验证
                userNameLabel.setText("账号不能为空");
                return;
            } else if (pwdField.getText().isEmpty()) {
                pwdLabel.setText("密码不能为空");
                return;
            } else if (!pwdField.getText().equals(RePwdField.getText())) {
                rePwdLabel.setText("密码不一致");
                return;
            }else if (nameField.getText().length()>10){
                userNameLabel.setText("账号长度不能大于10");
                return;
            }else if (pwdField.getText().length()>10){
                rePwdLabel.setText("密码长度不能大于10");
                return;
            }
            User user = new User();
            user.setUsername(nameField.getText()); // 将文本框中的数据实例化到用户中
            user.setPwd(pwdField.getText());
            UserDAO userDAO = new UserDAO();
            String flag = userDAO.getRegister(user); // 调用注册方法
            if (flag.equals("已存在")) {
                userNameLabel.setText("账号已存在");
            } else if (flag.equals("insertNO")) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("系统维护中...");
                alert.setHeaderText("用户注册失败！系统维护中...");
                alert.show();
            } else {
                GameLoginUserStage gameLoginUserStage = new GameLoginUserStage();
                gameLoginUserStage.show();
                this.close();
                GameConstant.username=user.getUsername(); // 将注册的用户存入全局变量，登录框取
            }
        });
        exBtn.setOnMouseClicked(e -> { // 取消按钮鼠标点击事件
            GoApplicationStage.stage.show(); // 显示程序模式选择窗口
            this.close();
        });

        link.setOnMouseClicked(e -> { // 返回登录超链接鼠标点击事件
            GameLoginUserStage gameLoginUserStage = new GameLoginUserStage();
            gameLoginUserStage.show();
            this.close();
        });
        this.setOnCloseRequest(e -> { // 关闭窗口事件监听
            GameLoginUserStage gameLoginUserStage = new GameLoginUserStage();
            gameLoginUserStage.show();
            this.close();
        });
    }
}
